class Solution {
    public int minOperations(int[] nums, int x) {
        int left=0;
        int right=0;
        int len=99999;
        int sum=0;
        int total=0;

        for(int i=0;i<nums.length;i++){
            total+=nums[i];
        }

        if(total==x){
            return nums.length;
        }

        while(right<nums.length){
            sum+=nums[right];

            while(left<=right&&sum>=total-x){
                if(sum==total-x){
                    len=Math.min(len,left+nums.length-right-1);
                }
                sum-=nums[left++];

            }
            right++;
        }

        return len==99999?-1:len;

    }
}